/**
 * format date
 * inside use _date
 * outside use static date
 */
export default function formatDate(timestamp, fmt)  {
	let date = new Date(timestamp);
	if(!date || !timestamp || date == "Invalid Date")
		return "";
	fmt = fmt || "yyyy-MM-dd";
    if (/(y+)/.test(fmt))
    	fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
    let o = new DateOption(date);
    let keys = Object.getOwnPropertyNames(DateOption.prototype);
    for(let k of keys) {
    	if (new RegExp("(" + k + ")").test(fmt))
    		fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    }
    return fmt;
}

/**
 * regex date options
 */
function DateOption(date) {
	this.date = date;
	Object.freeze(this);
}
Object.defineProperties(DateOption.prototype, {
	"M+" : { get(){ return this.date.getMonth() + 1; }},
	"d+" : { get(){ return this.date.getDate(); }},
	"h+" : { get(){ return this.date.getHours(); }},
	"H+" : { get(){ return this.date.getHours(); }},
	"m+" : { get(){ return this.date.getMinutes(); }},
	"s+" : { get(){ return this.date.getSeconds(); }},
	"q+" : { get(){ return Math.floor((this.date.getMonth() + 3) / 3); }},
	"S" : { get(){ return this.date.getMilliseconds(); }}
});
Object.freeze(DateOption);
Object.freeze(DateOption.prototype);